package com.dddframework.core.utils;

import cn.hutool.core.collection.CollUtil;
import com.dddframework.core.context.BaseContext;
import com.dddframework.core.contract.constant.ContextConstants;

import java.util.ArrayList;
import java.util.List;

public class ExceptionKit {

    public static String getProjectStackTraces(Throwable e) {
        ArrayList<StackTraceElement> stackTraceElements = CollUtil.toList(e.getStackTrace());
        List<String> result = new ArrayList<>();
        String projectPackage = BaseContext.get(ContextConstants.PROJECT_PACKAGE);
        for (StackTraceElement s : stackTraceElements) {
            String fileName = s.getFileName();
            int lineNumber = s.getLineNumber();
            // 忽略条件
            if (fileName == null || lineNumber == -1) continue;
            if (s.getClassName().startsWith(projectPackage) || s.getClassName().startsWith("com.dddframework")) {
                result.add(fileName + "(" + lineNumber + ")");
            }
        }
        return String.join("; ", result);
    }
}
